package main

import (
	"context"
	consul "github.com/asim/go-micro/plugins/registry/consul/v4"
	opentracing2 "github.com/go-micro/plugins/v4/wrapper/trace/opentracing"
	"github.com/opentracing/opentracing-go"
	"go-micro.dev/v4/web"
	//"goproduct/common"
	common "git.mashibing.com/msb_47094/shopping-comm"
	"goproduct/proto"
	"log"
	"strconv"

	"github.com/gin-gonic/gin"
	"go-micro.dev/v4"
	"go-micro.dev/v4/registry"
)

// 获取远程服务的客户端
func main() {
	router := gin.Default()

	router.Handle("GET", "toPage", func(context *gin.Context) {
		context.String(200, "to toPage ....")
	})

	//注册到consul
	consulReg := consul.NewRegistry(func(options *registry.Options) {
		options.Addrs = []string{common.ConsulIp + ":8500"}
	})
	//初始化链路追踪的jaeper
	t, io, err := common.NewTracer("shop-product-client", common.ConsulIp+":6831")
	if err != nil {
		log.Println(err)
	}
	defer io.Close()
	opentracing.SetGlobalTracer(t)

	rpcServer := micro.NewService(
		//micro.Name("shop-product-client"),
		micro.Registry(consulReg), //服务发现
		micro.WrapClient(opentracing2.NewClientWrapper(opentracing.GlobalTracer())),
	)
	client := proto.NewPageService("shop-product", rpcServer.Client())
	//分页查询商品列表
	router.GET("/page", func(c *gin.Context) {
		//获取远程服务的客户端 client
		//获取页面参数
		length, _ := strconv.Atoi(c.Request.FormValue("length"))
		pageIndex, _ := strconv.Atoi(c.Request.FormValue("pageIndex"))
		//拼接请求信息
		req := &proto.PageReq{
			Length:    int32(length),
			PageIndex: int32(pageIndex),
		}
		//远程调用服务
		resp, err := client.Page(context.TODO(), req)
		log.Println(" /page  :", resp)
		//根据响应做输出
		if err != nil {
			log.Println(err.Error())
			//c.String(http.StatusBadRequest, "search failed !")
			common.RespFail(c.Writer, resp, "请求失败")
			return
		}
		////writer  data  message  row  total  field
		common.RespListOK(c.Writer, resp, "请求成功", resp.Rows, resp.Total, "请求成功")
	})
	//查询商品详情
	clientA := proto.NewShowProductDetailService("shop-product", rpcServer.Client())
	router.GET("/showProductDetail", func(c *gin.Context) {
		//获取远程服务的客户端 client
		//获取页面参数
		id, _ := strconv.Atoi(c.Request.FormValue("id"))
		//拼接请求信息
		req := &proto.ProductDetailReq{
			Id: int32(id),
		}
		//远程调用服务
		resp, err := clientA.ShowProductDetail(context.TODO(), req)
		log.Println(" /showProductDetail  :", resp)
		//根据响应做输出
		if err != nil {
			log.Println(err.Error())
			//c.String(http.StatusBadRequest, "search failed !")
			common.RespFail(c.Writer, resp, "请求失败")
			return
		}
		////writer  data  message  row  total  field
		common.RespOK(c.Writer, resp, "请求成功")
	})
	//查询商品SKU
	clientSKU := proto.NewShowProductSkuService("shop-product", rpcServer.Client())
	router.GET("/sku", func(c *gin.Context) {
		//获取远程服务的客户端 client
		//获取页面参数
		id, _ := strconv.Atoi(c.Request.FormValue("productId"))
		//拼接请求信息
		req := &proto.ProductSkuReq{
			ProductId: int32(id),
		}
		//远程调用服务
		resp, err := clientSKU.ShowProductSku(context.TODO(), req)
		log.Println(" /sku  :", resp)
		//根据响应做输出
		if err != nil {
			log.Println(err.Error())
			//c.String(http.StatusBadRequest, "search failed !")
			common.RespFail(c.Writer, resp, "请求失败")
			return
		}
		////writer  data  message  row  total  field
		common.RespListOK(c.Writer, resp, "请求成功", 0, 0, "请求成功")

	})
	service := web.NewService(
		web.Address(":6667"),
		web.Name("shop-product-client"),
		web.Registry(consulReg),
		web.Handler(router),
	)
	service.Run()
	//router.Run(":6666")
}
